﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace LightSO.ILibrary
{
    /// <summary>
    /// 定义数据仓储公共接口
    /// （实现接口需在Web.Config配置Spring容器，指定创建实例的工厂类型与程序集）
    /// </summary>
    public partial interface IRepository<T> : InterfaceBase where T : EntityBase, new()
    {
        /// <summary>
        /// 支持Linq语法查询结果集对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        IQueryable<T> AsQueryable();

        /// <summary>
        /// 根据条件表达式统计满足条件的数据条数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        long Count(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 根据条件表达式判断是否存在符合条件的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        bool Exists(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 根据条件表达式获取满足条件的前n条类型为T的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <param name="n">需获取的记录数</param>
        /// <returns></returns>
        List<T> Top(Expression<Func<T, bool>> expression, int n);

        /// <summary>
        /// 根据条件表达式获取满足条件的前n条类型为T自定义排序数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <param name="n">需获取的记录数</param>
        /// <param name="orderby">排序对象集合</param>
        /// <returns></returns>
        List<T> Top(Expression<Func<T, bool>> expression, int n, params Expression<Func<T, bool>>[] orderby);

        /// <summary>
        /// 根据条件表达式获取满足条件的前n条类型为T自定义排序数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <param name="n">需获取的记录数</param>
        /// <param name="orderby">排序对象集合</param>
        /// <returns></returns>
        List<T> Top(Expression<Func<T, bool>> expression, int n, params Expression<Func<T, object>>[] groupby);

        /// <summary>
        /// 根据主键获取类型为T的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        T FindOne(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 根据条件表达式获取满足条件的所有类型为T的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        List<T> Find(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 根据条件表达式获取满足条件的所有类型为T的自定义排序数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <param name="orderby">排序对象集合</param>
        /// <returns></returns>
        List<T> Find(Expression<Func<T, bool>> expression, params Expression<Func<T, bool>>[] orderby);

        /// <summary>
        /// 根据条件表达式获取满足条件的所有类型为T的自定义分组数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件表达式</param>
        /// <param name="orderby">排序对象集合</param>
        /// <returns></returns>
        List<T> Find(Expression<Func<T, bool>> expression, params Expression<Func<T, object>>[] groupby);

        /// <summary>
        /// 获取指定分页标准为T的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数据量</param>
        /// <returns></returns>
        IPageList<T> FindPage(int pageIndex, int pageSize);

        /// <summary>
        /// 根据条件表达式获取满足条件、指定分页标准的所有类型为T的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数据量</param>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        IPageList<T> FindPage(int pageIndex, int pageSize, Expression<Func<T, bool>> expression);

        /// <summary>
        /// 根据条件表达式获取满足条件、指定分页标准的所有类型为T的自定义排序数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数据量</param>
        /// <param name="expression">条件表达式</param>
        /// <param name="sortList">排序对象集合</param>
        /// <returns></returns>
        IPageList<T> FindPage(int pageIndex, int pageSize, Expression<Func<T, bool>> expression, params Expression<Func<T, bool>>[] orderby);

        /// <summary>
        /// 根据条件表达式获取满足条件、指定分页标准的所有类型为T的自定义分组数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数据量</param>
        /// <param name="expression">条件表达式</param>
        /// <param name="orderby">排序对象集合</param>
        /// <returns></returns>
        IPageList<T> FindPage(int pageIndex, int pageSize, Expression<Func<T, bool>> expression, params Expression<Func<T, object>>[] groupby);
        /// <summary>
        /// 保存新的类型为T的数据对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">需保存的对象</param>
        /// <returns></returns>
        T Insert(T item);

        /// <summary>
        /// 更新指定的类型为T的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">需更新的对象</param>
        void Update(T item, Expression<Func<T, bool>> exp);

        ///<summary>
        /// 保存指定的类型为T的对象
        /// (找到符合条件的第一条数据并更新，没有符合条件的数据则插入)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">需更新的对象</param>
        /// <param name="exp">条件表达式</param>
		void Save(T item, Expression<Func<T, bool>> exp);

        /// <summary>
        /// 删除某个类型为T的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <param name="exp"></param>
        void Remove(Expression<Func<T, bool>> exp);
    }
}


